package com.ansatsing.juc.executor.scenario4;

import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

public class Main {
    public static void main(String[] args) {
        ScheduledExecutorService scheduledExecutor = Executors.newScheduledThreadPool(1);
        Task task = new Task("name");
        ScheduledFuture<?> result = scheduledExecutor.scheduleAtFixedRate(task,1,2,TimeUnit.SECONDS);
        for(int i=0;i<10;i++){
            System.out.printf("Main: Delay: %d \n",result.getDelay(TimeUnit.MILLISECONDS));//距离下个任务到来的时间
            try {
                TimeUnit.MILLISECONDS.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        scheduledExecutor.shutdown();//停止定时任务
        try {
          TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.printf("Main: Ends at: %s\n",new Date());
    }
}
